package bjtu.cit.bookshop.controller;

import bjtu.cit.bookshop.common.ErrorCode;

import javax.servlet.http.HttpSession;

/**
 * Created by jia19 on 2015/9/18.
 */
public class BaseController {

    public static final ErrorCode SUCCESS = new ErrorCode(0, "成功！");
    public static final ErrorCode NO_AUTH = new ErrorCode(1, "没有相应权限！");
    public static final ErrorCode BAD_PASSWORD = new ErrorCode(2, "密码错误！");
    public static final ErrorCode USER_EXIST = new ErrorCode(3, "用户名已经存在！");
    public static final ErrorCode INFORMATION_MISSING = new ErrorCode(4, "信息不完整！");
    public static final ErrorCode PARA_ERROR = new ErrorCode(5, "参数错误！");


    protected String checkAdminLogin(HttpSession session) {
        String isLogin_str = (String) session.getAttribute("isLogin");
        String isAdmin = (String)session.getAttribute("admin");
        String loginName = (String) session.getAttribute("loginName");

        if (isLogin_str != null && isLogin_str.equals("true") && isAdmin != null && isAdmin.equals("true")) {
            return loginName;
        }
        else {
            return null;
        }
    }

    protected String checkLogin(HttpSession session) {
        String isLogin_str = (String) session.getAttribute("isLogin");
        String isAdmin = (String)session.getAttribute("admin");
        String loginName = (String) session.getAttribute("loginName");

        if (isLogin_str != null && isLogin_str.equals("true") && (isAdmin == null ||  isAdmin.equals("false"))) {
            return loginName;
        }
        else {
            return null;
        }
    }

}
